package pl.praca.magisterska.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import pl.praca.magisterska.service.PersonService;

@Controller
@SessionAttributes
public class LogoutController {

	
	private static Logger log = Logger.getLogger(LogoutController.class);
	
	@Autowired
	private PersonService service;
	
	@Autowired
	private Validator validator;
	
	/**
	 * @return the service
	 */
	public PersonService getService() {
		return service;
	}

	/**
	 * @param service the service to set
	 */
	public void setService(PersonService service) {
		this.service = service;
	}

	/**
	 * @return the validator
	 */
	public Validator getValidator() {
		return validator;
	}

	/**
	 * @param validator the validator to set
	 */
	public void setValidator(Validator validator) {
		this.validator = validator;
	}
	
//	@RequestMapping(value = "/logout", method = RequestMethod.POST)
//	public String addContact(@ModelAttribute("contact") Contact contact,
//			BindingResult result) {
//		
//		log.info("post");
//		
//		validator.validate(contact, result);  
//	    if (result.hasErrors()) { return "logoutView"; } 
//		return "redirect:addContact.html";
//	}

      
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(true);
		session.removeAttribute("person");
		log.info("get");
					
		return new ModelAndView("logoutView");
				
	}
}
